package com.cma.cmabackend.service;

import com.cma.cmabackend.dao.ProjectDao;
import com.cma.cmabackend.entity.LabEmp;
import com.cma.cmabackend.entity.Parameter;
import com.cma.cmabackend.entity.Project;
import com.cma.cmabackend.entity.Standard;
import com.cma.cmabackend.entity.in.InStandard;
import com.cma.cmabackend.entity.re.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectService {
    @Resource
    ProjectDao projectDao;

    @Resource
    StandardService standardService;

    @Resource
    LabAdminService labAdminService;

    public Project addProject(String name, String reason, Integer creater_id, List<InStandard> inStandardList) {
        Project project = new Project();
        project.setReason(reason);
        project.setName(name);
        project.setStandards(new ArrayList<>());
        project.setStatus(Project.Status.Progressing);
        project.setLabAdmin(labAdminService.findById(creater_id));

        for (InStandard i : inStandardList) {
            Standard standard = standardService.add(i.getCategory(), i.getType(), i.getName(), i.getFile_uuid(),i.getIid());
            project.getStandards().add(standard);
        }

        return projectDao.save(project);
    }

    public List<ReProject> getAllProgressingAndCompleted() {
        return projectDao.findAllByStatus(Project.Status.Progressing, Project.Status.Completed);
    }

    public ReDetailProject findById(int id) {
        Project project = projectDao.findById(id).orElse(null);
        if (project == null) {
            return null;
        }
        ReDetailProject reDetailProject = new ReDetailProject(
                project.getId(),
                project.getName(),
                project.getReason(),
                project.getStatus(),
                new ReUser(project.getLabAdmin().getId(), project.getLabAdmin().getType(), project.getLabAdmin().getUsername()),
                new ArrayList<>()
        );

        for (Standard i : project.getStandards()) {
            ReStandard reStandard = new ReStandard(
                    i.getId(),
                    i.getCategoryId(),
                    i.getTypeId(),
                    i.getFileUuid(),
                    i.getName(),
                    new ArrayList<>(),
                    i.getSample(),
                    i.getSop(),
                    i.getSimulation()
            );
            for (Parameter p : i.getParameterList()) {
                ReParameter reParameter = new ReParameter(
                        p.getId(),
                        p.getName(),
                        new ArrayList<>(),
                        p.getHumanList(),
                        p.getEquipmentList(),
                        p.getSample(),
                        p.getSop(),
                        p.getFacilityList(),
                        p.getCompareValidList(),
                        p.getSimulation(),
                        p.getStatus()
                );
                for (LabEmp u : p.getEmps()) {
                    ReUser reUser = new ReUser(
                            u.getId(),
                            u.getType(),
                            u.getUsername()
                    );
                    reParameter.getEmps().add(reUser);
                }
                reStandard.getParameterList().add(reParameter);
            }
            reDetailProject.getStandards().add(reStandard);
        }
        return reDetailProject;
    }

    public List<ReProject> getAllConfirming() {
        return projectDao.findAllByState(Project.Status.Confirming);
    }

    public Project findByStandard(Standard standard) {
        return projectDao.findByStandardsContains(standard);
    }

    public Project save(Project project) {
        return projectDao.save(project);
    }

    public void setStatus(Integer id, Project.Status status) {
        Project project = projectDao.findById(id).orElse(null);
        if (project == null) {
            return;
        }
        project.setStatus(status);
        projectDao.save(project);
    }

    public Project findRealById(Integer id) {
        return projectDao.findById(id).orElse(null);
    }

    public List<ReProject> getAllConfirmingAndConfirmedAndAgreed() {
        return projectDao.findAllBy3Status(Project.Status.Confirming, Project.Status.Confirmed, Project.Status.Agreed);
    }

    public List<ReProject> getAllConfirmedAndAgreed() {
        return projectDao.findAllByStatus(Project.Status.Confirmed, Project.Status.Agreed);
    }
}
